{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']\n",
      " ['1986' 'Americas' 'Uruguay' 'Other' '0.5']\n",
      " ['1985' 'Africa' \"Cte d'Ivoire\" 'Wine' '1.62']\n",
      " ..., \n",
      " ['1987' 'Africa' 'Malawi' 'Other' '0.75']\n",
      " ['1989' 'Americas' 'Bahamas' 'Wine' '1.5']\n",
      " ['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "world_alcohol = np.genfromtxt(\"world_alcohol.txt\", delimiter=',', dtype=str, skip_header=1)# 读取txt文档\n",
    "print(type(world_alcohol))\n",
    "print(world_alcohol)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4]\n",
      "[[ 5 10 15]\n",
      " [20 25 30]\n",
      " [35 40 45]]\n"
     ]
    }
   ],
   "source": [
    "vector = np.array([1, 2, 3, 4])\n",
    "matrix = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])\n",
    "print(vector)\n",
    "print(matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4,)\n",
      "(3, 3)\n",
      "int32\n"
     ]
    }
   ],
   "source": [
    "print(vector.shape)\n",
    "print(matrix.shape)\n",
    "print(matrix.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5\n",
      "Cte d'Ivoire\n"
     ]
    }
   ],
   "source": [
    "uruguay_other_1986 = world_alcohol[1, 4]# 通过索引查找数据\n",
    "third_country = world_alcohol[2, 2]\n",
    "print(uruguay_other_1986)\n",
    "print(third_country)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 5 10 15]\n"
     ]
    }
   ],
   "source": [
    "vector = np.array([5, 10, 15, 20])\n",
    "print(vector[0:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 25 40]\n",
      "[[ 5 10]\n",
      " [20 25]\n",
      " [35 40]]\n"
     ]
    }
   ],
   "source": [
    "matrix = np.array([\n",
    "    [5, 10, 15],\n",
    "    [20, 25, 30],\n",
    "    [35, 40, 45]\n",
    "])\n",
    "print(matrix[:, 1])\n",
    "print(matrix[:, 0:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False  True False False]\n",
      "[[False False False]\n",
      " [False  True False]\n",
      " [False False False]]\n"
     ]
    }
   ],
   "source": [
    "print(vector==10)\n",
    "print(matrix==25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10]\n"
     ]
    }
   ],
   "source": [
    "equal_to_ten = (vector == 10)# 通过boolean类型取值\n",
    "print(vector[equal_to_ten])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False  True False]\n",
      "[[20 25 30]]\n"
     ]
    }
   ],
   "source": [
    "second_column_25 = (matrix[:, 1]==25)\n",
    "print(second_column_25)\n",
    "print(matrix[second_column_25, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False False False False]\n",
      "[ True  True False False]\n"
     ]
    }
   ],
   "source": [
    "equal_to_ten_and_five = (vector==10)&(vector==5)\n",
    "equal_to_ten_or_five = (vector==10)|(vector==5)\n",
    "print(equal_to_ten_and_five)\n",
    "print(equal_to_ten_or_five)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<U1\n",
      "['1' '2' '3']\n",
      "float64\n",
      "[ 1.  2.  3.]\n"
     ]
    }
   ],
   "source": [
    "vector = np.array(['1', '2', '3'])\n",
    "print(vector.dtype)\n",
    "print(vector)\n",
    "vector = vector.astype(float)\n",
    "print(vector.dtype)\n",
    "print(vector)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
